package com.controller;

import java.io.File;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.entity.Ptype;
import com.server.PtypeServer;
import com.server.RecordServer;
import com.server.SysuserServer;
import com.util.PageBean;

@Controller
public class PtypeController {
	@Resource
	private PtypeServer ptypeService;
	@Resource
	private SysuserServer userService;
	@Resource
	private RecordServer recordService;

	@RequestMapping("admin/addPtype.do")//指标模块
	public String addPtype(HttpServletRequest request, Ptype Ptype, HttpSession session) {
		ptypeService.add(Ptype);
		return "redirect:ptypeList.do";
	}

	// 处理更新指标
	@RequestMapping("admin/doUpdatePtype.do")
	public String doUpdatePtype(ModelMap map, int id) {
		map.put("jz", ptypeService.getById(id));
		return "admin/ptype_update";
	}

	// 查看指标信息
	@RequestMapping("admin/ptype_detail.do")
	public String ptype_detail(ModelMap map, int id) {
		map.put("jz", ptypeService.getById(id));
		return "admin/ptype_detail";
	}

	// 保存指标信息
	@RequestMapping("admin/updatePtype.do")
	public String updatePtype(HttpServletRequest request, Ptype ptype) {
		ptypeService.update(ptype);
		return "redirect:ptypeList.do";
	}

	// 分页查询
	@RequestMapping("admin/ptypeList.do")
	public String jianList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session) {

		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap = new HashMap<String, Object>();
		int total = ptypeService.getCount(cmap);
		pageBean.setTotal(total);
		List<Ptype> list = ptypeService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		map.put("ulist", userService.getAll(null));
		return "admin/ptype_list";
	}

	
	@RequestMapping("admin/deletePtype.do")
	public String deletePtype(int id, HttpSession session) {
		ptypeService.delete(id);
		return "redirect:ptypeList.do";

	}

	// 文件上传
	public String fileUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, String img) {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa = request.getContextPath() + "/upload/" + fileName;
		System.out.println("path===" + pa);
		if (fileName != null && !fileName.equals("")) {
			img = fileName;
		} else {
			img = "zanwu.jpg";
		}
		return img;

	}
}
